<script>
	var url;
	$(document).ready(function(){
	
		newData = function (){
			$('#dialog').dialog({
				title: 'Tambah Data Dosen',
				width: 650,
				height: 450,
				closed: true,
				cache: false,
				href: base_url+'dosen/add',
				modal: true
			});
			 
			$('#dialog').dialog('open');
			url = base_url+'dosen/save/add';
		}
		// end newData
		
		importDialog = function (){  
			$('#dlgimport_dosen').dialog('open').dialog('setTitle','Import Data Dosen');
			$('#fmimport_dosen').form('clear');  
			url = base_url+'dosen/import'; 
		}
		
		importData=function(){
			$('#fmimport_dosen').form('submit',{
				url: url,
				onSubmit: function(){
					//return $(this).form('validate');
				},
				success: function(result){
					//alert(result);
					var result = eval('('+result+')');
					if (result.success){
						 $.messager.show({
							title: 'Sukses',
							msg: result.msg
						}); 
						$('#dlgimport_dosen').dialog('close');		// close the dialog
						$('#dg').datagrid('reload');	// reload the user data
					} else {
						$.messager.show({
							title: 'Error',
							msg: result.msg
						});
					}
				}
			});
		}
		
		editData = function (val){
			//var row = $('#dg').datagrid('getSelected');
			//if (row){
				$('#dialog').dialog({
					title: 'Edit Data Dosen',
					width: 650,
					height: 450,
					closed: true,
					cache: false,
					href: base_url+'dosen/edit/'+val,
					modal: true
				});
				
				$('#dialog').dialog('open');  
				url = base_url+'dosen/save/edit';
			//}
		}
		//end editData
		
		deleteData = function (val){
			//var row = $('#dg').datagrid('getSelected');
			//if(row){
				if(confirm("Apakah yakin akan menghapus data '" + val + "'?")){
					var response = '';
					$.ajax({ type: "GET",
						 url: base_url+'dosen/delete/' + val,
						 async: false,
						 success : function(response){
							var response = eval('('+response+')');
							if (response.success){
								$.messager.show({
									title: 'Success',
									msg: 'Data Berhasil Dihapus'
								});
								// reload and close tab
								$('#dg').datagrid('reload');
							} else {
								$.messager.show({
									title: 'Error',
									msg: response.msg
								});
							}
						 }
					});
				}
			//}
		}
		//end deleteData 
		
		saveData = function(){
			$('#form1').form('submit',{
				url: url,
				onSubmit: function(){
					return $(this).form('validate');
				},
				success: function(result){
					//alert(result);
					var result = eval('('+result+')');
					if (result.success){
						$('#dialog').dialog('close');		// close the dialog
						$('#dg').datagrid('reload');		// reload the user data
					} else {
						$.messager.show({
							title: 'Error',
							msg: result.msg
						});
					}
				}
			});
		}
		//end saveData
		
		actionbutton = function(value, row, index){
			var col;
			//if (row.kd_fakultas != null) {
				<?if($this->mdl_auth->CekAkses(array('menu_id'=>15, 'policy'=>'EDIT'))){?>
				col = '<a href="#" onclick="editData(\''+row.kd_dosen+'\');" class="easyui-linkbutton" iconCls="icon-edit" plain="false">Edit</a>';
				<?}?>
				col += '&nbsp;&nbsp;|&nbsp;&nbsp;';
				<?if($this->mdl_auth->CekAkses(array('menu_id'=>15, 'policy'=>'DELETE'))){?>
				col += '&nbsp;&nbsp;|&nbsp;&nbsp;<a href="#" onclick="deleteData(\''+row.kd_dosen+'\');" class="easyui-linkbutton" iconCls="icon-edit" plain="false">Delete</a>';
				<?}?>
			//}
			return col;
		}
		
		// load grid
		$(function(){
			$('#dg').datagrid({url:"<?=base_url()?>dosen/grid"});	
			//$('#dg').datagrid('enableFilter'); 			
		});	
		
		// onchange
		$('#s_kd_fakultas').change(function(){
			$('#s_kd_prodi').load(base_url+'prosedur/getProdibyFakultas/'+$('#s_kd_fakultas').val());
		});
		
		// filter
		filter = function(){
			$('#dg').datagrid('load',{
				kd_fakultas : $('#s_kd_fakultas').val(),
				kd_prodi : $('#s_kd_prodi').val(),
				kd_dosen : $('#s_kd_dosen').val(),
				nama_dosen : $('#s_nama_dosen').val(),
			});
			//$('#dg').datagrid('enableFilter');
		}
		
		//# Tombol Bawah
		$(function(){
			var pager = $('#dg').datagrid().datagrid('getPager');	// get the pager of datagrid
			pager.pagination({
				buttons:[
					{
						iconCls:'icon-add',
						text:'Tambah Data',
						handler:function(){
							newData();
						}
					},
					{
						iconCls:'icon-print',
						text:'Export PDF',
						handler:function(){
						var kd_fakultas = $('#s_kd_fakultas').val();
						var kd_prodi = $('#s_kd_prodi').val();
						if(kd_prodi !=''){
								window.open('<?=base_url().'dosen/laporan_pdf'?>/'+$('#s_kd_prodi').val());
							}
							else{
								$.messager.show({
									title: 'Warning',
									msg: 'Harap Isi Filter Terlabih Dahulu'
								});
							}
						}
					},
					{
						iconCls:'icon-excel',
						text:'Export EXCEL',
						handler:function(){
							window.open('<?=base_url().'dosen/laporan_excel'?>');
						}
					},
					{
						iconCls:'icon-upload',
						text:'Import EXCEL',
						handler:function(){
							importDialog();
							//window.open('<?=base_url().'jenjang/import'?>');
						}
					}	
				]
			});			
		});
		
		//filter import
		$('#kd_fakultas2').change(function(){
			$('#kd_prodi2').load(base_url+'prosedur/getProdibyFakultas/'+$('#kd_fakultas2').val());
		});
		
	});
</script>

<table id="dg" title="Kelola Data Dosen" data-options="
			rownumbers:true,
			singleSelect:true,
			autoRowHeight:false,
			pagination:true,
			pageSize:30,
			fit:true,
			toolbar:'#toolbar',
			">
	<thead>
		<tr>
			<!--<th field="tahun_penerimaan" sortable="true" width="80">Tahun Penerimaan</th> -->
			<th field="kd_dosen" sortable="true" width="90">Kode Dosen</th>
			<th field="nidn" sortable="true" width="90">NIDN</th>
			<th field="nama_dosen" sortable="true" width="180">Nama Dosen</th>
			<th field="alamat" sortable="true" width="150">Alamat</th>
			<th field="telepon" sortable="true" width="90">Telepon</th>
			<th field="pendidikan_s1" sortable="true" width="120">Pendidikan S1</th>
			<th field="pendidikan_s2" sortable="true" width="120">Pendidikan S2</th>
			<th field="action" align="center" formatter="actionbutton" width="100">Aksi</th>
		</tr>
	</thead>
</table>
<div id="toolbar" style="padding:5px;height:auto">
	<div style="margin-bottom:5px">
		
	</div>
	<div class="fsearch">
		<table width="700" border="0">
		  <tr>
			<td>Fakultas</td>
			<td>: 
				<select id="s_kd_fakultas" name="s_kd_fakultas" style="width:290px;">
					<?=$this->mdl_prosedur->OptionFakultas();?>
				</select>
			</td>
			<td>Kode Dosen</td>
				<td>: 
					<input name="s_kd_dosen" id="s_kd_dosen" size="15">
				</td>
		  </tr>
		  <tr>
			<td>Program Studi</td>
			<td>: 
				<select id="s_kd_prodi" name="s_kd_prodi" style="width:290px;">
					<?=$this->mdl_prosedur->OptionProdi();?>
				</select>
			</td>
			
			<!--<td>&nbsp;</td>-->
			<td>Nama Dosen</td>
				<td>: 
					<input name="s_nama_dosen" id="s_nama_dosen" size="15">
				</td>
			<td>&nbsp;</td>
		  </tr>
		  <tr>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
				<td>&nbsp;</td>				
				<td>&nbsp;&nbsp;<a href="#" onclick="filter()" class="easyui-linkbutton" iconCls="icon-search">Filter</a></td>
		  </tr>
		</table>
	</div>
</div>

<!-- FORM IMPORT -->
<div id="dlgimport_dosen" class="easyui-dialog" style="width:500px;height:200px;padding:10px 20px" closed="true" buttons="#dlg-buttons">
	<div class="ftitle">Import Data Dosen dari Excel</div>
	<?php 
		$attributes = array('id' => 'fmimport_dosen');
		echo form_open_multipart('', $attributes);
	?>
		<div class="fitem">
		<label style="width:100px">Fakultas</label>
			<select id="kd_fakultas2" name="kd_fakultas" style="width:200px;">
					<?=$this->mdl_prosedur->OptionFakultas(array('value'=>$kd_fakultas));?>
			</select>
		</div>	
				
		<div class="fitem" >
				<label style="width:100px">Prodi</label>
				<select id="kd_prodi2" name="kd_prodi" style="width:200px;">
				</select>
		</div>	
		<div class="fitem">
			<label style="width:100px;vertical-align:top">File</label>
			<input type="file" name="datafile"/>
		</div>
	</form>
	<div id="dlg-buttons">
		<a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="importData()">Upload</a>
		<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlgimport_dosen').dialog('close')">Cancel</a>
	</div>
</div>